﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.SQLite;
using System.IO;

namespace DataBaseAccess
{
    // http://blog.tigrangasparian.com/2012/02/09/getting-started-with-sqlite-in-c-part-one/
    class DataBaseCreation
    {
        private DataBaseConnection _conn;

        public DataBaseCreation(DataBaseConnection conn)
        {
            _conn = conn;
        }
        
        private void DeleteDatabase()
        {
            if (File.Exists(DbConstants.DbName + DbConstants.DbExtention))
            {
                try
                {
                    File.Delete(DbConstants.DbName + DbConstants.DbExtention);
                }
                catch
                {
                    throw new Exception("Could not delete database file.");
                }
            }
        }

        private void CreateDataBase()
        {
            if (!File.Exists(DbConstants.DbName))
            {
                SQLiteConnection.CreateFile(DbConstants.DbName + DbConstants.DbExtention);
            }
            if (File.Exists("DB_Creation.txt"))
            {
                List<string> sql = new List<string>();
                sql.Add(File.ReadAllText("DB_Creation.txt"));
                _conn.ExecuteCommandNoReturn(sql);
            }
        }

        public void CreateNewDatabase()
        {
            DeleteDatabase();
            CreateDataBase();
        }
    }
}
